package com.xfhy.wallpaperdemo;

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;

public class CWallpaperService extends WallpaperService {

    public class a extends WallpaperService.Engine {
        public a() {
            //super(CWallpaperService.this);
        }

        private int a() {
            return getWallPaperPreviewRes();
        }

        private int getWallPaperPreviewRes() {
            Log.d("xfhy_wall","getWallPaperPreviewRes");
            return R.drawable.wallpaper_common;
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
            Log.d("xfhy_wall","onSurfaceChanged");
            super.onSurfaceChanged(surfaceHolder, i, i2, i3);
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder surfaceHolder) {
            Log.d("xfhy_wall","onSurfaceCreated");
            Bitmap a2;
            super.onSurfaceCreated(surfaceHolder);

            //画图
            Canvas lockCanvas = surfaceHolder.lockCanvas();
            if (lockCanvas != null && (a2 = a(lockCanvas.getWidth(), lockCanvas.getHeight())) != null && !a2.isRecycled()) {
                Bitmap scaledBitmap = Bitmap.createScaledBitmap(a2, lockCanvas.getWidth(), lockCanvas.getHeight(), true);
                lockCanvas.drawBitmap(scaledBitmap, 0.0f, 0.0f, new Paint());
                surfaceHolder.unlockCanvasAndPost(lockCanvas);
            }
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder surfaceHolder) {
            super.onSurfaceDestroyed(surfaceHolder);
            Log.d("xfhy_wall","onSurfaceDestroyed");
        }

        @Override
        public void onVisibilityChanged(boolean z2) {
            Log.d("xfhy_wall","ResetWallpaperService onVisibilityChanged,visible=" + z2);
        }

        private Bitmap a(int i, int i2) {
            if (isPreview()) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 1;
                options.inPreferredConfig = Bitmap.Config.RGB_565;
                try {
                    return BitmapFactory.decodeResource(MyApplication.sContext.getResources(), a(), options);
                } catch (Throwable unused) {
                    return null;
                }
            } else {
                try {
                    return ((BitmapDrawable) WallpaperManager.getInstance(MyApplication.sContext).getDrawable()).getBitmap();
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("xfhy_wall","CleanWallpaperService onCreate");
    }

    @Override
    public WallpaperService.Engine onCreateEngine() {
        Log.d("xfhy_wall","CleanWallpaperService onCreateEngine");
        return new a();
    }

    @Override
    public void onDestroy() {
        Log.d("xfhy_wall","CleanWallpaperService onDestroy");
        super.onDestroy();
    }
}

